//
//  YLMonthlyBillHeaderTopView.m
//  YLFinanceService
//
//  Created by she1323 on 2017/9/25.
//  Copyright © 2017年 she1323. All rights reserved.
//

#import "YLMonthlyBillHeaderTopView.h"
#import <Masonry.h>
#import "YLGlobeHeader.h"
#import "YBPopupMenu.h"
#import "WHDefaultNotificationCenter.h"
@interface YLMonthlyBillHeaderTopView ()<YBPopupMenuDelegate>
@property (nonatomic, weak) UIButton *showMoreTimeButton;
@property (nonatomic, strong) UIImageView *arrow;
@property (nonatomic, weak) UILabel *timeLabel;
@property (nonatomic, strong) NSMutableArray *months;
@property (nonatomic, strong) NSMutableArray *dates;
@end

@implementation YLMonthlyBillHeaderTopView

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        [self setUp];
    }
    return self;
}

- (void)setUp
{
    __weak typeof(self) weakSelf = self;
    UIView *line = [[UIView alloc] init];
    line.backgroundColor = YLGayColor;
    line.alpha = 0.2;
    [self addSubview:line];
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(weakSelf).mas_offset(15);
        make.right.mas_equalTo(weakSelf).mas_offset(-15);
        make.height.mas_equalTo(1);
        make.bottom.mas_equalTo(weakSelf);
    }];
    
    UIButton *drawingBoardButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [drawingBoardButton setBackgroundImage:[UIImage imageNamed:@"MyDrawingBoard"] forState:UIControlStateNormal];
    [self addSubview:drawingBoardButton];
    [drawingBoardButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(weakSelf);
        make.left.mas_equalTo(line);
        make.height.width.mas_equalTo(22);
    }];
    
    
    UIImageView *arrow = [[UIImageView alloc] init];
    _arrow = arrow;
    arrow.contentMode = UIViewContentModeScaleAspectFit;
    [self addSubview:arrow];
    arrow.image = [UIImage imageNamed:@"down"];
    [arrow mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(weakSelf);
        make.right.mas_equalTo(line);
        make.width.height.mas_equalTo(15);
    }];
    
    UILabel *timeLabel = [[UILabel alloc] init];
    
    
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
    [dateFormatter setDateFormat:@"yyyy年MM月"];
    
    timeLabel.text = [self getMonth:1];
    timeLabel.textColor = YLLineColor;
    _timeLabel = timeLabel;
    [self addSubview:timeLabel];
    [timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(arrow.mas_left).mas_offset(-5);
        make.centerY.mas_equalTo(weakSelf);
    }];
    
    
    UIButton *showMoreTimeButton = [UIButton buttonWithType:UIButtonTypeCustom];
    _showMoreTimeButton = showMoreTimeButton;
    [self addSubview:showMoreTimeButton];
    showMoreTimeButton.backgroundColor = [UIColor clearColor];
    [showMoreTimeButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(timeLabel);
        make.top.bottom.right.mas_equalTo(weakSelf);
    }];
    
    [[drawingBoardButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
        [[NSNotificationCenter defaultCenter] postNotificationName:@"YLMonthlyBillCalendar" object:nil];
    }];
    
    [[showMoreTimeButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
        [YBPopupMenu showRelyOnView:timeLabel titles:weakSelf.months icons:nil menuWidth:120 delegate:self];
        
        [UIView animateWithDuration:0.25 animations:^{
            
            showMoreTimeButton.selected = !showMoreTimeButton.selected;
            
            /*
             以下三点结论基于未旋转的情况：
             1.当参数x>0 && x<=M_PI时,为顺时针
             2.当参数x>-M_PI && x<0时,为逆时针
             3.若参数x<M_PI || x>2.0*M_PI时,则旋转方向等同于x%2的旋转方向
             总结：旋转方向就是向最短路径方向旋转
             */
            
            if (showMoreTimeButton.selected == YES) {
                arrow.transform = CGAffineTransformMakeRotation(M_PI);// 顺时针旋转180度
            } else {
                arrow.transform = CGAffineTransformMakeRotation(2 * M_PI);// 顺时针旋转180度
            }
            
        }];
        
     
    }];
    
    _timeLabel.text = self.months.firstObject;
    
    [WHDefaultNotificationCenter wh_postEventToNotificationName:@"YLChangeTimeNoti" object:self.dates.firstObject];
    
    
}


- (NSMutableArray *)months
{
    if (!_months) {
        _months = [NSMutableArray arrayWithCapacity:5];
    }
    
    // 为了防止第一天生成 第二天出错 不能设为懒加载
    [_months removeAllObjects];
    [_dates removeAllObjects];
    
    for (NSInteger i = 0; i < 5; i ++) {
        [_months addObject: [self getMonth:i+1]];
    }
    
    
    return _months;
}

- (NSMutableArray *)dates
{
    if (!_dates) {
        _dates = [NSMutableArray arrayWithCapacity:5];
    }
    return _dates;
}




// 得到几个月
- (NSString *)getMonth:(NSInteger)month
{
    NSCalendar *calender2 = [[NSCalendar alloc]initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    
    [calender2 setFirstWeekday:2];// 国外是从周日 开始算的 我们是周一 所以 写了2
    
    //NSDateComponents *components2 = nil;
    
    NSDate *currentDate = [NSDate date];
    
    
//    NSTimeZone * zone = [NSTimeZone systemTimeZone];
//    
//    NSInteger interval = [zone secondsFromGMTForDate:currentDate];
//    
//    NSDate * nowDate = [currentDate dateByAddingTimeInterval:interval];
    
 //   components2 = [calender2 components:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay fromDate:currentDate];
    
    NSDateComponents *adcomps = [[NSDateComponents alloc]init];
    
    [adcomps setYear:0];
    
    [adcomps setMonth:-month];
    
    
    [adcomps setDay:0];
    
    NSDate *newdate = [calender2 dateByAddingComponents:adcomps toDate:currentDate options:0];
    
    [self.dates addObject:newdate];
    
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
    [dateFormatter setDateFormat:@"yyyy年MM月"];
    
    return [dateFormatter stringFromDate:newdate];
}


- (void)ybPopupMenuBeganDismiss
{
    
    [UIView animateWithDuration:0.25 animations:^{
        
        _showMoreTimeButton.selected = !_showMoreTimeButton.selected;
        /*
         以下三点结论基于未旋转的情况：
         1.当参数x>0 && x<=M_PI时,为顺时针
         2.当参数x>-M_PI && x<0时,为逆时针
         3.若参数x<M_PI || x>2.0*M_PI时,则旋转方向等同于x%2的旋转方向
         总结：旋转方向就是向最短路径方向旋转
         */
        
        if (_showMoreTimeButton.selected == YES) {
            _arrow.transform = CGAffineTransformMakeRotation(M_PI);// 顺时针旋转180度
        } else {
            _arrow.transform = CGAffineTransformMakeRotation(2 * M_PI);// 顺时针旋转180度
        }
    }];
}

/**
 点击事件回调
 */
- (void)ybPopupMenuDidSelectedAtIndex:(NSInteger)index ybPopupMenu:(YBPopupMenu *)ybPopupMenu
{
    _timeLabel.text = self.months[index];
    
    [WHDefaultNotificationCenter wh_postEventToNotificationName:@"YLChangeTimeNoti" object:self.dates[index]];
}

@end
